package com.cmict.data.infra.controller.admin.source;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.framework.common.pojo.CommonResult;
import static com.cmict.data.framework.common.pojo.CommonResult.success;

import com.cmict.data.framework.excel.core.util.ExcelUtils;

import com.cmict.data.framework.operatelog.core.annotations.OperateLog;
import static com.cmict.data.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.cmict.data.infra.controller.admin.source.vo.*;
import com.cmict.data.infra.dal.dataobject.source.SourceDO;
import com.cmict.data.infra.convert.source.SourceConvert;
import com.cmict.data.infra.service.source.SourceService;

@Api(tags = "管理后台 - 数据源")
@RestController
@RequestMapping("/infra/source")
@Validated
public class SourceController {

    @Resource
    private SourceService sourceService;

    @PostMapping("/create")
    @ApiOperation("创建数据源")
    @PreAuthorize("@ss.hasPermission('sys:source:create')")
    public CommonResult<Long> createSource(@Valid @RequestBody SourceCreateReqVO createReqVO) {
        return success(sourceService.createSource(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新数据源")
    @PreAuthorize("@ss.hasPermission('sys:source:update')")
    public CommonResult<Boolean> updateSource(@Valid @RequestBody SourceUpdateReqVO updateReqVO) {
        sourceService.updateSource(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/batchDelete")
    @ApiOperation("批量删除数据源")
    @PreAuthorize("@ss.hasPermission('sys:source:delete')")
    public CommonResult<Boolean> batchDeleteSource(@RequestBody Collection<Long> ids) {
        sourceService.batchDeleteSource(ids);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除数据源")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('sys:source:delete')")
    public CommonResult<Boolean> deleteSource(@RequestParam("id") Long id) {
        sourceService.deleteSource(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得数据源")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('sys:source:query')")
    public CommonResult<SourceRespVO> getSource(@RequestParam("id") Long id) {
        SourceDO source = sourceService.getSource(id);
        return success(SourceConvert.INSTANCE.convert(source));
    }

    @GetMapping("/list")
    @ApiOperation("获得数据源列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('sys:source:query')")
    public CommonResult<List<SourceRespVO>> getSourceList(@RequestParam("ids") Collection<Long> ids) {
        List<SourceDO> list = sourceService.getSourceList(ids);
        return success(SourceConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得数据源分页")
    @PreAuthorize("@ss.hasPermission('sys:source:query')")
    public CommonResult<PageResult<SourceRespVO>> getSourcePage(@Valid SourcePageReqVO pageVO) {
        PageResult<SourceRespVO> pageResult = sourceService.getSourcePage(pageVO);
        return success(pageResult);
    }

    @GetMapping("/listchoose")
    @ApiOperation("获得数据源选择列表")
    //@PreAuthorize("@ss.hasPermission('sys:source:query')")
    public CommonResult<List<SourceDO>> getSourceListChoose() {
        List<SourceDO> list = sourceService.getSourceList();
        return success(list);
    }
    @GetMapping("/listchooseByExcludeId")
    @ApiOperation("获得数据源选择列表")
    @PreAuthorize("@ss.hasPermission('sys:source:query')")
    public CommonResult<List<SourceDO>> getSourceListChoose(Long excludeId) {
        List<SourceDO> list = sourceService.getListchooseByExcludeId(excludeId);
        return success(list);
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出数据源 Excel")
    @PreAuthorize("@ss.hasPermission('sys:source:export')")
    @OperateLog(type = EXPORT)
    public void exportSourceExcel(@Valid SourceExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<SourceDO> list = sourceService.getSourceList(exportReqVO);
        // 导出 Excel
        List<SourceExcelVO> datas = SourceConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "数据源.xls", "数据", SourceExcelVO.class, datas);
    }

}
